package com.baigt.juc.base;

import java.util.concurrent.TimeUnit;

/**
 * 类功能描述:
 * synchronized的特点
 *
 * @author baigt
 * @version V1.0
 * @since 2019-10-21 10:25
 */
public class ThreadRun05 {
    public static void main(String[] args) throws InterruptedException {
        ThreadRun05 threadRun05 = new ThreadRun05();
        new Thread(threadRun05::method001, "this 001").start();
        new Thread(threadRun05::method002, "this 002").start();
    }

    public synchronized void method001() {
        sleep();
    }

    public synchronized void method002() {
        sleep();
    }

    public static synchronized void method011() {
        sleep();
    }

    public static synchronized void method012() {
        sleep();
    }

    public static synchronized void sleep() {
        try {
            TimeUnit.MINUTES.sleep(4);
        } catch (InterruptedException e) {
            System.out.println("error ....");
        }
    }

    /**
     * static 方式的synchronized 信息
     * new Thread(ThreadRun05::method011,"static 011").start();
     * new Thread(ThreadRun05::method012,"static 012").start();
     * ----------------------------------------------------------------------
     * C:\Users\Administrator>jstack 9532
     * 2019-10-22 11:20:13
     * Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode):
     * <p>
     * "DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x0000000002e42800 nid=0x1c8c waiting on condition [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "static 012" #12 prio=5 os_prio=0 tid=0x000000001de69800 nid=0x2da8 waiting on condition [0x000000001e6df000]
     * java.lang.Thread.State: TIMED_WAITING (sleeping)
     * at java.lang.Thread.sleep(Native Method)
     * at java.lang.Thread.sleep(Thread.java:340)
     * at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
     * at com.ricky.juc.base.ThreadRun05.sleep(ThreadRun05.java:39)
     * - locked <0x000000076b5df360> (a java.lang.Class for com.ricky.juc.base.ThreadRun05)
     * at com.ricky.juc.base.ThreadRun05.method012(ThreadRun05.java:34)
     * - locked <0x000000076b5df360> (a java.lang.Class for com.ricky.juc.base.ThreadRun05)
     * at com.ricky.juc.base.ThreadRun05$$Lambda$2/2003749087.run(Unknown Source)
     * at java.lang.Thread.run(Thread.java:745)
     * <p>
     * "static 011" #11 prio=5 os_prio=0 tid=0x000000001de65000 nid=0x20e0 waiting for monitor entry [0x000000001e5df000]
     * java.lang.Thread.State: BLOCKED (on object monitor)
     * at com.ricky.juc.base.ThreadRun05.method011(ThreadRun05.java:31)
     * - waiting to lock <0x000000076b5df360> (a java.lang.Class for com.ricky.juc.base.ThreadRun05)
     * at com.ricky.juc.base.ThreadRun05$$Lambda$1/1915318863.run(Unknown Source)
     * at java.lang.Thread.run(Thread.java:745)
     * <p>
     * "Service Thread" #10 daemon prio=9 os_prio=0 tid=0x000000001dba5000 nid=0x2e18 runnable [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "C1 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001db1f800 nid=0x1efc waiting on condition [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001db1e000 nid=0x2a8c waiting on condition [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001db1a800 nid=0xe7c waiting on condition [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001db18000 nid=0x2c54 runnable [0x000000001dfde000]
     * java.lang.Thread.State: RUNNABLE
     * at java.net.SocketInputStream.socketRead0(Native Method)
     * at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
     * at java.net.SocketInputStream.read(SocketInputStream.java:170)
     * at java.net.SocketInputStream.read(SocketInputStream.java:141)
     * at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
     * at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
     * at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
     * - locked <0x000000076b716e08> (a java.io.InputStreamReader)
     * at java.io.InputStreamReader.read(InputStreamReader.java:184)
     * at java.io.BufferedReader.fill(BufferedReader.java:161)
     * at java.io.BufferedReader.readLine(BufferedReader.java:324)
     * - locked <0x000000076b716e08> (a java.io.InputStreamReader)
     * at java.io.BufferedReader.readLine(BufferedReader.java:389)
     * at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)
     * <p>
     * "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001c58f800 nid=0x3160 waiting on condition [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001c58e800 nid=0x2e50 runnable [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000002f37000 nid=0x28fc in Object.wait() [0x000000001d8cf000]
     * java.lang.Thread.State: WAITING (on object monitor)
     * at java.lang.Object.wait(Native Method)
     * - waiting on <0x000000076b306f58> (a java.lang.ref.ReferenceQueue$Lock)
     * at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
     * - locked <0x000000076b306f58> (a java.lang.ref.ReferenceQueue$Lock)
     * at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
     * at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
     * <p>
     * "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000002f2e000 nid=0x2e84 in Object.wait() [0x000000001d7cf000]
     * java.lang.Thread.State: WAITING (on object monitor)
     * at java.lang.Object.wait(Native Method)
     * - waiting on <0x000000076b306998> (a java.lang.ref.Reference$Lock)
     * at java.lang.Object.wait(Object.java:502)
     * at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)
     * - locked <0x000000076b306998> (a java.lang.ref.Reference$Lock)
     * <p>
     * "VM Thread" os_prio=2 tid=0x000000001c547800 nid=0x3274 runnable
     * <p>
     * "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002e57800 nid=0x2cec runnable
     * <p>
     * "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002e59000 nid=0x1578 runnable
     * <p>
     * "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002e5b000 nid=0xbe0 runnable
     * <p>
     * "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002e5c800 nid=0x3088 runnable
     * <p>
     * "VM Periodic Task Thread" os_prio=2 tid=0x000000001db94800 nid=0x4a8 waiting on condition
     * <p>
     * JNI global references: 332
     */
    void staticsyn() {
    }

    /**
     * ThreadRun05 threadRun05=new ThreadRun05();
     * new Thread(threadRun05::method001,"this 001").start();
     * new Thread(threadRun05::method002,"this 002").start();
     * ---------------------------------------------------------
     * C:\Users\Administrator>jstack 8960
     * 2019-10-22 11:26:36
     * Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.45-b02 mixed mode):
     * <p>
     * "DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x0000000003052800 nid=0x321c waiting on condition [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "this 002" #12 prio=5 os_prio=0 tid=0x000000001e040800 nid=0xcb4 waiting for monitor entry [0x000000001e8af000]
     * java.lang.Thread.State: BLOCKED (on object monitor)
     * at com.ricky.juc.base.ThreadRun05.method002(ThreadRun05.java:29)
     * - waiting to lock <0x000000076b5e2f30> (a com.ricky.juc.base.ThreadRun05)
     * at com.ricky.juc.base.ThreadRun05$$Lambda$2/295530567.run(Unknown Source)
     * at java.lang.Thread.run(Thread.java:745)
     * <p>
     * "this 001" #11 prio=5 os_prio=0 tid=0x000000001dfe5000 nid=0x2ea0 waiting on condition [0x000000001e7af000]
     * java.lang.Thread.State: TIMED_WAITING (sleeping)
     * at java.lang.Thread.sleep(Native Method)
     * at java.lang.Thread.sleep(Thread.java:340)
     * at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
     * at com.ricky.juc.base.ThreadRun05.sleep(ThreadRun05.java:40)
     * - locked <0x000000076b5dff58> (a java.lang.Class for com.ricky.juc.base.ThreadRun05)
     * at com.ricky.juc.base.ThreadRun05.method001(ThreadRun05.java:26)
     * - locked <0x000000076b5e2f30> (a com.ricky.juc.base.ThreadRun05)
     * at com.ricky.juc.base.ThreadRun05$$Lambda$1/1915318863.run(Unknown Source)
     * at java.lang.Thread.run(Thread.java:745)
     * <p>
     * "Service Thread" #10 daemon prio=9 os_prio=0 tid=0x000000001dd6c800 nid=0xf08 runnable [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "C1 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001dd66800 nid=0x678 waiting on condition [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001dcf6800 nid=0x2f14 waiting on condition [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001dcf4000 nid=0x2a50 waiting on condition [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001dcf9000 nid=0x1b44 runnable [0x000000001e1ae000]
     * java.lang.Thread.State: RUNNABLE
     * at java.net.SocketInputStream.socketRead0(Native Method)
     * at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
     * at java.net.SocketInputStream.read(SocketInputStream.java:170)
     * at java.net.SocketInputStream.read(SocketInputStream.java:141)
     * at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
     * at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
     * at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
     * - locked <0x000000076b7176d8> (a java.io.InputStreamReader)
     * at java.io.InputStreamReader.read(InputStreamReader.java:184)
     * at java.io.BufferedReader.fill(BufferedReader.java:161)
     * at java.io.BufferedReader.readLine(BufferedReader.java:324)
     * - locked <0x000000076b7176d8> (a java.io.InputStreamReader)
     * at java.io.BufferedReader.readLine(BufferedReader.java:389)
     * at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)
     * <p>
     * "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001c79b800 nid=0x174 waiting on condition [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001c74e800 nid=0x1728 runnable [0x0000000000000000]
     * java.lang.Thread.State: RUNNABLE
     * <p>
     * "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000003149000 nid=0x1518 in Object.wait() [0x000000001daaf000]
     * java.lang.Thread.State: WAITING (on object monitor)
     * at java.lang.Object.wait(Native Method)
     * - waiting on <0x000000076b306f58> (a java.lang.ref.ReferenceQueue$Lock)
     * at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
     * - locked <0x000000076b306f58> (a java.lang.ref.ReferenceQueue$Lock)
     * at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
     * at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
     * <p>
     * "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000003140000 nid=0x33b4 in Object.wait() [0x000000001d9ae000]
     * java.lang.Thread.State: WAITING (on object monitor)
     * at java.lang.Object.wait(Native Method)
     * - waiting on <0x000000076b306998> (a java.lang.ref.Reference$Lock)
     * at java.lang.Object.wait(Object.java:502)
     * at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)
     * - locked <0x000000076b306998> (a java.lang.ref.Reference$Lock)
     * <p>
     * "VM Thread" os_prio=2 tid=0x000000001c707800 nid=0x3310 runnable
     * <p>
     * "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000003067800 nid=0x306c runnable
     * <p>
     * "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000003069000 nid=0x2fb0 runnable
     * <p>
     * "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x000000000306b000 nid=0x3284 runnable
     * <p>
     * "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x000000000306c800 nid=0x32d8 runnable
     * <p>
     * "VM Periodic Task Thread" os_prio=2 tid=0x000000001dd70800 nid=0x2f08 waiting on condition
     * <p>
     * JNI global references: 333
     */
    void thissynchronized() {
    }

}
